package com.situ.mall.admin.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.situ.mall.common.entity.Attachment;
import com.situ.mall.common.entity.Product;
import com.situ.mall.common.service.IAttachmentService;
import com.situ.mall.common.service.IProductService;
import com.situ.mall.common.vo.EditProductVO;
import com.situ.mall.common.vo.TableVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private IProductService productService;
    @Autowired
    private IAttachmentService attachmentService;

    //打开商品管理的页面
    @GetMapping("/page")
    public String page() {
        return "product";
    }

    @GetMapping("/list")
    @ResponseBody
    public TableVO list(Integer page, Integer limit, String name, String status) {
        return productService.selectList(page, limit, name, status);
    }

    @PostMapping("/delete")
    @ResponseBody
    public void delete(String ids) {
        productService.delete(ids);
    }

    @PostMapping("/save")
    @ResponseBody
    public void save(Product product) {
        productService.saveOrUpdate(product);
    }

    @GetMapping("/{id}")
    @ResponseBody
    public EditProductVO info(@PathVariable Integer id) {
        return productService.edit(id);
    }

    @PostMapping("/status")
    @ResponseBody
    public void status(Integer id, String status) {
        productService.update(new LambdaUpdateWrapper<Product>().eq(Product::getId, id).set(Product::getStatus, status));
    }

    @PostMapping("/bind")
    @ResponseBody
    public void bind(Integer id, String attIds) {
        attachmentService.bind(attIds, id, "product");
    }

    @GetMapping("/attachment/{id}")
    @ResponseBody
    public List<Attachment> attachmentList(@PathVariable Integer id) {
        return attachmentService.selectList("product", id);
    }
}
